package cn.xspace.wnx.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.ServiceObjBean;

/**
 * Created by zhuyuhuan on 2017/3/20 0020.
 */
public class ServiceObjSearchAdapter  extends BaseAdapter {

    private List<ServiceObjBean> dataList;
    private Context mContext;

    public ServiceObjSearchAdapter(Context mContext, List<ServiceObjBean> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public long getItemId(int position) {
        return dataList.get(position).hashCode();
    }

    @Override
    public ServiceObjBean getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ServiceObjBean bean = getItem(position);
        ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.service_obj_item, null);
            mViewHolder.findView(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        setData(mViewHolder,bean,position);
        return convertView;
    }

    private void setData(ViewHolder viewHolder, ServiceObjBean bean, int position) {
        HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, viewHolder
                .headerIV, HeadShowUtil.HeadType.UserLogo);
        viewHolder.serviceObjName.setText(bean.user_name);
        viewHolder.serviceObjTypeName.setText(bean.object_typename);
        viewHolder.userAddress.setText(bean.user_address);
    }

    static class ViewHolder {
        ImageView headerIV;
        TextView contactItemAlphaTV;
        TextView serviceObjName;
        TextView serviceObjTypeName;
        TextView userAddress;
        ImageView mLineImg;

        public void findView(View v) {
            this.headerIV = (RoundAngleImageView) v.findViewById(R.id.riv_service_obj_head);
            this.contactItemAlphaTV = (TextView) v.findViewById(R.id.contact_list_item_alpha_service);
            this.serviceObjName = (TextView) v.findViewById(R.id.tv_service_obj_name);
            this.serviceObjTypeName = (TextView) v.findViewById(R.id.tv_object_typename);
            this.userAddress = (TextView) v.findViewById(R.id.tv_user_address);
            this.mLineImg = (ImageView) v.findViewById(R.id.line_ing_service);
        }
    }
}
